<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Order Success - VCEX</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<script src="https://cdn.tailwindcss.com"></script>

		<!-- Google tag (gtag.js) -->
		<script async src="https://www.googletagmanager.com/gtag/js?id=G-3EZRWBC905"></script>
		<script>
		  window.dataLayer = window.dataLayer || [];
		  function gtag(){dataLayer.push(arguments);}
		  gtag('js', new Date());
		
		  gtag('config', 'G-3EZRWBC905');
		</script>
	</head>
	<body class="bg-gray-100 min-h-screen flex items-center justify-center p-3">

		<main id="order-container" class="max-w-2xl w-full bg-white rounded-xl shadow-lg p-3 space-y-6">
			<!-- ✅ Success Header -->
			<div class="text-center space-y-4">
				<div class="flex justify-center">
					<div class="w-20 h-20 flex items-center justify-center rounded-full bg-green-100 shadow">
						<svg xmlns="http://www.w3.org/2000/svg" class="w-10 h-10 text-green-600" fill="none"
							viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
						</svg>
					</div>
				</div>
				<h1 class="text-2xl font-bold text-gray-800">Payment Successful</h1>
				<!-- 去掉 text-center，交给内部结构控制对齐 -->
				<div id="order-success-msg" class="bg-green-50 border border-green-200 rounded-lg p-2"></div>
			</div>

			<!-- 🛒 Order Summary -->
			<section id="order-summary" class="border-t pt-4"></section>

			<!-- 🚚 Shipping & Total -->
			<section id="order-total" class="border-t pt-4 space-y-1"></section>

			<!-- 📦 Shipping Info -->
			<section id="shipping-info" class="border-t pt-4"></section>

			<!-- 🔢 Order Number -->
			<section id="order-no" class="border-t pt-4"></section>

			<!-- 📩 Contact -->
			<section class="border-t pt-6 text-center space-y-2">
				<h2 class="text-lg font-bold text-gray-800">Need Assistance?</h2>
				<p class="text-gray-600 text-sm">
					Our support team is here to help you with any questions about your order.
				</p>
				<a href="mailto:contact@vitacorex.com"
					class="inline-block px-4 py-2 bg-blue-600 text-white text-sm font-semibold rounded-lg shadow hover:bg-blue-700 active:bg-blue-800 transition">
					📧 contact@vitacorex.com
				</a>
				<p class="text-gray-500 text-xs">We typically respond within 24 hours.</p>
			</section>

			<!-- 🔙 Back Button -->
			<div class="text-center pt-4">
				<a href="index.html"
					class="inline-block px-6 py-3 bg-green-600 text-white font-bold rounded-lg shadow hover:bg-green-700 active:bg-green-800">
					Back to Home
				</a>
			</div>

			<!-- 🏢 Company Footer -->
			<footer class="mt-8 text-center text-xs text-gray-500 border-t pt-4">
				<p>© 2025 LeadMob All rights reserved.</p>
				<p class="mt-1">Your trusted partner in health & wellness.</p>
			</footer>
		</main>

		<script>
			// 从 URL 获取 session_id 参数
			const urlParams = new URLSearchParams(window.location.search)
			const sessionId = urlParams.get('session_id')

			if (sessionId) {
				const apiUrl = `https://vitacorex.com/vcex/order/success?session_id=${sessionId}`
				console.log('调用接口：', apiUrl)

				fetch(apiUrl)
					.then(res => res.json())
					.then(res => {
						if (res.code !== 0) throw new Error(res.msg || '接口错误')

						const data = res.data
						const product = data.product_list && data.product_list[0] ? data.product_list[0] : {}
						const ship = data.shiping_info || {}

						// ✅ 成功提示：左对齐信息卡片 + 两条要点，清晰不臃肿
						const emailDisplay = ship.email || 'your email'
						const emailHref = ship.email ? `mailto:${ship.email}` : 'mailto:'
						document.getElementById('order-success-msg').innerHTML = `
              <div class="flex gap-2">
                <div class="text-green-600">
                  <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                  </svg>
                </div>
                <div class="space-y-2 text-left">
                  <p class="text-green-700 font-semibold">Your order was placed successfully!</p>
                  <ul class="space-y-1 text-gray-800 text-sm">
                    <li class="flex items-start gap-2">
                      <span>📦 Order will be delivered within <span class="font-semibold">3–7 business days</span>.</span>
                    </li>
                    <li class="flex items-start gap-2">
                      <span>📧 A confirmation email will be sent to
                        <a href="${emailHref}" class="text-blue-600 font-semibold hover:underline">${emailDisplay}</a>
                        within <span class="font-semibold">5 minutes</span>.
                      </span>
                    </li>
                  </ul>
                </div>
              </div>
            `

						// 🛒 Order Summary
						document.getElementById('order-summary').innerHTML = `
              <h2 class="font-bold text-lg mb-3">Order Summary</h2>
              <div class="flex gap-4 items-center">
                <img src="${product.image_url || ''}" alt="${product.name || 'Product'}"
                     class="w-20 h-20 border rounded-lg object-contain">
                <div class="flex-1">
                  <p class="font-semibold text-gray-800">${(product.name || '')} ${(product.sub_title || '')}</p>
                  <p class="text-sm text-gray-600">Unit Price: $${product.price || '0.00'}</p>
                  <p class="text-sm text-gray-600">${product.buy_tip || ''}</p>
                </div>
                <p class="text-lg font-bold text-gray-900">$${product.total_price || '0.00'}</p>
              </div>
            `

						// 🚚 Total（写死 FREE SHIPPING）
						document.getElementById('order-total').innerHTML = `
              <div class="flex justify-between items-center text-sm py-2 border-b">
                <span class="text-gray-600">Shipping</span>
                <span class="px-3 py-1 rounded-full bg-green-100 text-green-700 font-semibold text-xs shadow-sm">
                  FREE SHIPPING
                </span>
              </div>
              <div class="flex justify-between font-bold text-lg text-gray-800 pt-3">
                <span>Total</span>
                <span class="text-red-600">$${product.total_price || '0.00'}</span>
              </div>
            `

						// 📦 Shipping Info
						document.getElementById('shipping-info').innerHTML = `
              <h2 class="font-bold text-lg mb-3">Shipping Information</h2>
              <p class="text-sm text-gray-700">Name: ${ship.first_name || ''} ${ship.last_name || ''}</p>
              <p class="text-sm text-gray-700">Address: ${ship.line1 || ''} ${ship.line2 || ''}, ${ship.city || ''}, ${ship.state || ''} ${ship.postal_code || ''}, ${ship.country || ''}</p>
              <p class="text-sm text-gray-700">Email: ${ship.email || ''}</p>
              <p class="text-sm text-gray-700">Phone: ${ship.phone || ''}</p>
            `

						// 🔢 Order Number & Time
						function formatOrderTime(raw) {
							if (!raw) return ''
							const d = new Date(raw.replace(/-/g, '/')) // Safari 兼容
							return d.toLocaleString('en-US', {
								year: 'numeric',
								month: 'short',
								day: '2-digit',
								hour: '2-digit',
								minute: '2-digit',
								hour12: true
							})
						}
						document.getElementById('order-no').innerHTML = `
              <h2 class="font-bold text-lg mb-2">Order Details</h2>
              <p class="text-sm text-gray-700">Order Number: <span class="font-mono">${data.order_no || ''}</span></p>
              <p class="text-sm text-gray-700">Order Time: <span class="font-mono">${formatOrderTime(data.created_time)}</span></p>
            `


					})
					.catch(err => {
						console.error('接口请求出错：', err)
						document.getElementById('order-container').innerHTML = `
              <div class="text-center text-red-600 font-bold">Failed to load order information.</div>
            `
					})
			} else {
				console.warn('当前URL没有 session_id 参数')
			}
		</script>
	</body>
</html>